from google.appengine.ext import db

class Drinker(db.Model):
	google_plus_id = db.StringProperty(required=False)
	last_drink = db.IntegerProperty(required=False)
	favorite_beer = db.IntegerProperty(required=False)
	google_account = db.UserProperty(required=False)
	kegdroids = db.StringProperty()
	
	def to_dict(self):
		SIMPLE_TYPES = (int, long, float, bool, dict, basestring, list)
		output = {}
		output["ID"] = self.key().id()
		for key, prop in self.properties().iteritems():
			value = getattr(self, key)
			if isinstance(value, SIMPLE_TYPES):
				output[key] = value
			elif value is None:
				output[key] = ""
			elif isinstance(value, datetime.date):
				# Convert date/datetime to ms-since-epoch ("new Date()").
				ms = time.mktime(value.utctimetuple())
				ms += getattr(value, 'microseconds', 0) / 1000
				output[key] = int(ms)
			elif isinstance(value, db.Model):
				output[key] = to_dict(value)
			else:
				raise ValueError('cannot encode ' + repr(prop))
		return output